package com.example.controller;

import com.example.pojo.Order;
import com.example.pojo.Query;
import com.example.service.OrderService;
import com.example.utils.Page;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("selectOrderList")
    public Result selectOrderList(Query query){//分页查询订单信息
        Page<Order> page=new Page<>(query.getPageNum(),query.getPageSize(),orderService.selectOrderCount(query.getClassification()));
        page.setList(orderService.selectOrderList(query.getClassification(),page.getStart(),query.getPageSize()));
        Map<String,Object> map=new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());//总条数
        map.put("pageSize", query.getPageSize());//每页显示几条
        map.put("pageNum", query.getPageNum());//当前页码
        return new Result<>("200", "查询成功", map);
    }

    @GetMapping("selectOrderById")
    public Result selectOrderById(int id){//根据id查询订单信息
        Order order=orderService.selectOrderById(id);
        Map<String,Object> map=new HashMap<>();
        if (order!=null){
            map.put("code", "200");
            map.put("data", order);
            map.put("msg", "查询成功");
        }else {
            map.put("code", "444");
            map.put("data", null);
            map.put("msg", "查询失败");
        }
        return new Result("222", "调用成功", map);
    }
}
